import { data_cols2 } from '../../config/data'
import config from '../../config/app'
import { get_date } from '../utils/date'
import { emit } from '../services/socketio'

// 设置classname函数
export const setClassName = (target, str, value) => {
    if (target.className.search(str) !== -1) {
        target.className = value
    } else {
        target.parentNode.className = value
    }
}
// 公共拖动函数
export const drop = (e, m, dispatch, state) => {

    // 判断是哪个元素接受拖入
    let self = e.dataTransfer.getData("Text")
    let op = document.getElementById(self).parentNode
    let target


    if (e.target.className.search('table') !== -1) {
        if (e.target.id) {
            target = e.target
        } else {
            target = e.target.parentNode
        }
    } else {
        if (e.target.id.search('tag') !== -1) {
            target = e.target
        } else {
            target = e.target.parentNode
        }
    }

    if (target.children.length < m) {
        if (target !== op) {
            console.log('拖入目标：', target, '拖出来源：', op, '自身：', self)

            var tmp_file, tmp_table1, tmp_table2

            if (target.className.search('file_tags') !== -1) {
                console.log('需要添加file_tags')

                tmp_file = [...state.file_data_cols]
                tmp_file.push(self)

            } else {
                console.log('需要添加table_drop_box', target.id)

                tmp_table1 = [...state.tag_bind[target.id]]
                tmp_table1.push(self)

            }

            if (op.className.search('file_tags') !== -1) {
                console.log('需要减少file_tags')

                tmp_file = [...state.file_data_cols]
                tmp_file = tmp_file.filter(i => i !== self)
                tmp_file = tmp_file.filter(i => i)

            } else {
                console.log('需要减少table_drop_box', op.id)

                tmp_table2 = [...state.tag_bind[op.id]]
                tmp_table2 = tmp_table2.filter(i => i !== self)

            }

            var data = Object.assign({}, state.tag_bind)
            if (tmp_table1) data[target.id] = tmp_table1
            if (tmp_table2) data[op.id] = tmp_table2

            dispatch({
                type: 'global/save', payload: {
                    tag_bind: data,
                    file_data_cols: tmp_file ? tmp_file : state.file_data_cols
                }
            })
        }
    }

}
// 保存绑定标签
export const save_tags = (state, dispatch) => {
    console.log('save')
    let list = []
    state.file_data.forEach((row, rid) => {
        let str = { key: rid }
        for (const key in state.tag_bind) {
            str[key] = state.tag_bind[key].length > 1 ? state.tag_bind[key].map(k => row[k]) : row[state.tag_bind[key][0]] ? row[state.tag_bind[key][0]] : ''
        }
        list.push(str)
    })
    dispatch({ type: 'global/save', payload: { format_file_data: list } })
}
// 格式化上传数据
export const format_data = (rowkeys, state, dispatch) => {
    let sub_cols = { xzrq: get_date(), op: `来自文件：${state.upload_file} 的[${state.sheet}]工作表` }
    let keys = Object.keys(data_cols2)
    keys.forEach(col => {
        sub_cols[col] = ''
    })
    let upload_file = rowkeys.map(id => {
        return { ...state.format_file_data[id], ...sub_cols }
    })
    dispatch({ type: 'global/save', payload: { upload_file_data: upload_file } })

    return upload_file

}

// 保存到服务器
export const save_list_to_service = (upload_file_data) => {
    console.log("save_list_to_service")
    emit({ type: 'save_list', data: [config.list_db, upload_file_data] })
}
// 重置绑定标签
export const reset_tags = (state, dispatch) => {
    let tags = {}
    state.keys.forEach(key => {
        tags[key] = []
    })
    let file_data_cols = state.file_cols.map(i => i.title)

    dispatch({
        type: 'global/save',
        payload: {
            tag_bind: tags,
            file_data_cols: file_data_cols,
            format_file_data: [],
            upload_file_data: [],
        }
    })
}

export const rowSelection = (keys, state, dispatch) => {
    dispatch({ type: 'global/save', payload: { srowkey: keys } })
    console.log(state.srowkey)
}